package com.guanzi.big_data_mq_process.controller;

import com.guanzi.big_data_mq_process.model.dto.OrdersDTO;
import com.guanzi.big_data_mq_process.model.entity.OrderDetails;
import com.guanzi.big_data_mq_process.producer.OrderProducerService;
import com.guanzi.big_data_mq_process.service.ExcelImportService;
import com.guanzi.big_data_mq_process.service.IUserOrderService;
import com.guanzi.big_data_mq_process.service.IUsersService;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private OrderProducerService orderService;

    @Autowired
    private IUsersService usersService;

    @Autowired
    private ExcelImportService excelImportService;

    @GetMapping("/send")
    public void test() {
        // 随机生成数字
        String uuid = UUID.randomUUID().toString();
        int randomUserId = RandomUtils.nextInt(0, 100000);
        int randomNumber = RandomUtils.nextInt(0, 20);

        // 测试创建订单
        OrdersDTO dto = new OrdersDTO();
        dto.setUserId(randomUserId);
        List<OrderDetails> orderDetails = new ArrayList<>();
        for (int i = 0; i < randomNumber; i++) {
            orderDetails.add(new OrderDetails("【" + uuid + "】测试商品_" + i, RandomUtils.nextInt(0, 5), BigDecimal.valueOf(RandomUtils.nextDouble(0, 50000))));
        }

        // 求总金额
        dto.setTotalAmount(orderDetails.stream().map(OrderDetails::getPrice).reduce(BigDecimal.ZERO, BigDecimal::add));
        dto.setDetails(orderDetails);

        orderService.createOrderTransactionMessage(dto);
    }

    /**
     * 测试大数据Excel导出
     * @param response
     * @throws Exception
     */
    @GetMapping("/testExportBigData")
    public String testExportBigData(HttpServletResponse response) throws Exception {
        usersService.exportData(response);
        return "ok";
    }

    /**
     * 测试大数据量Excel导入
     * @param file
     */
    @PostMapping("/testImportBigData")
    public String testImportBigData(@RequestPart("file") MultipartFile file) throws IOException {
        excelImportService.testImportBigData(file);
        return "ok";
    }

}
